<?php

namespace App\Http\Controllers\Guard\Teacher;

use App\Http\Controllers\Common\ApiController;
use App\Models\Teacher\Teacher;
use App\Service\TeacherService;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class TeacherController extends ApiController {
    private $service;

    public function __construct() {
        $this->service = new TeacherService(Teacher::class);
    }

    public function change(Request $request) {
        $id = $request->input('id',0);
        if($request->has('status')){
            $this->service->update($id, 'status', $request->input('status'));
        }elseif ($request->has('is_top')){
            $this->service->update($id, 'is_top', $request->input('is_top'));
        }else{

        }
        return responseSuccess();

    }

    public function lst(Request $request) {
        $conditions = $request->input();
        $records = $this->service->lst($conditions)
            ->paginate($conditions['pageSize']??20,['*'],'page',$conditions['page']);
        foreach ($records as $record) {
            $record->pic_url = asset($record->pic_url);
            $record->content = Str::limit($record->content,50);
        }

        return responseSuccess($records->items(),$records->total());
    }

    public function add(Request $request) {

        $this->validate($request, [
            'name'=>'required|min:1|max:20',
            'pic_url'=>'required',
            'content'=>'required'
        ],[],[
            'name'=>'院校名称',
            'pic_url'=>'图片',
            'content'=>'教师介绍',
        ]);

        $info = $request->input();

        $token = $request->header('token');
        if(empty($token)) return responseFail('获取token失败！');
        $id = checkJWT($token)['id']??0;
        if($id<=0){
            return responseFail('获取当前用户失败！');
        }
        $info['guard_user_id'] = $id;

        $this->service->add($info);
        return responseSuccess();
    }

    public function detail(Request $request) {
        $id = $request->input('id',0);
        $record = $this->service->detail($id);
        return responseSuccess([$record->toArray()],1);
    }

    public function del(Request $request) {
        $ids = $request->input('ids',[]);
        $this->service->del($ids);
        return responseSuccess();
    }
}
